/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   FrameworkConfig.cs
 * CreateData   :   2023/7/11 15:46:13
 * UnityVersion :   2021.3.20f1c1
 * Description  :   框架组件配置表
************************************************/

#if UNITY_EDITOR
using UnityEditor;
#endif

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Sirenix.OdinInspector;
using System.Linq;

#pragma warning disable CS0414
namespace Main
{
    //[CreateAssetMenu(fileName ="创建资源")]
    public class FrameworkConfig : SerializedScriptableObject
    {
        [SerializeField]
#if UNITY_EDITOR
        [Title("Body")]
        [TableList(ShowIndexLabels = true, AlwaysExpanded = false), LabelText("框架预制件")]
#endif
        private List<FrameworkInfo> datas = new List<FrameworkInfo>();
        public FrameworkInfo this[string name]
        {
            get
            {
                return datas.FirstOrDefault<FrameworkInfo>(info => info.name == name);
            }
        }
        public FrameworkInfo this[int index]
        {
            get
            {
                return datas[index];
            }
        }
#if UNITY_EDITOR
        [Button("添加"), HorizontalGroup("Button")]
        private void Add()
        {
            FrameworkInfo info = new FrameworkInfo();
            datas.Add(info);
        }
        [Button("清空Null"), HorizontalGroup("Button")]
        private void ClearNull()
        {
            for (int i = 0; i < datas.Count;)
            {
                FrameworkInfo info = datas[i];
                if (info.prefab)
                {
                    i++;
                }
                else
                {
                    datas.RemoveAt(i);
                }
            }
        }
        [Button("清空"), HorizontalGroup("Button")]
        private void Clear()
        {
            datas.Clear();
        }
#endif
    }
    [Serializable]
    public class FrameworkInfo
    {
#if UNITY_EDITOR
        private void PrefabChange()
        {
            if (prefab)
            {
                MonoBehaviour mono = null;
                prefab.TryGetComponent<MonoBehaviour>(out mono);
                if (mono)
                {
                    name = prefab.name;
                }
                else
                {
                    name = "None";
                    EditorUtility.DisplayDialog("警告", "预制件未挂载MonoBehaviour脚步", "OK");
                    prefab = null;
                }
            }
            else
            {
                name = "None";
                prefab = null;
            }
        }
        [VerticalGroup("Prefab"), HideLabel, TableColumnWidthAttribute(100, Resizable = false),
            PreviewField(100, ObjectFieldAlignment.Center), AssetsOnly, OnValueChanged("PrefabChange")]
#endif
        public GameObject prefab;
#if UNITY_EDITOR
        [VerticalGroup("Name"), HideLabel, TableColumnWidthAttribute(150, Resizable = false), TextArea(6, 6)]
#endif
        public string name;
#if UNITY_EDITOR
        [VerticalGroup("Description"), HideLabel, TextArea(6, 6)]
#endif
        public string description;
    }
}
